<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>客户管理</el-breadcrumb-item>
                <el-breadcrumb-item>会员等级</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框杀杀杀
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="viplevelFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="viplevelFormData.name" placeholder="请输入等级名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary"@click="quere"> 查询</el-button>
                    <el-button type="primary" @click="insertClient">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="编号"
                            width="100">
                    </el-table-column>
                    <el-table-column
                            prop="name"
                            label="等级名称"
                            width="120">
                    </el-table-column>
                    <el-table-column
                            prop="ratio"
                            label="积分增减比"
                            width="120">
                    </el-table-column>
                    <el-table-column
                            prop="growthPoint"
                            label="成长值">
                    </el-table-column>
                    <el-table-column
                            prop="defaultStatus"
                            label="默认等级"
                            :formatter="statusFormat">
                    </el-table-column>
                    <el-table-column
                            prop="freeFreightPoint"
                            label="免运费标准"
                            width="120">
                    </el-table-column>
                    <el-table-column
                            prop="priviledgeComment"
                            label="评论获奖励特权"
                            width="120"
                            :formatter="commentFormat">
                    </el-table-column>
                    <el-table-column
                            prop="priviledgePromotion"
                            label="专享活动特权"
                            width="120"
                            :formatter="promotionFormat">
                    </el-table-column>
                    <el-table-column
                            prop="priviledgeMemberPrice"
                            label="会员价格特权"
                            width="120"
                            :formatter="memberPriceFormat">
                    </el-table-column>
                    <el-table-column
                            prop="priviledgeBirthday"
                            label="生日特权"
                            :formatter="birthdayFormat">
                    </el-table-column>
                    <el-table-column label="操作" width="120">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle @click="selectByVipId(scope.row.id)"></el-button>
                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteViplevel(scope.row.id)"></el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <!--                分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>
        </el-card>
        <!--添加客户的弹出框-->
        <el-dialog
                title="录入会员"
                :visible.sync="editDialogVisible"
                width="30%"
        >
            <!--            model:表单数据-->
            <el-form  label-width="120px" :model="viplevelForm"  :rules="rules" ref="viplevelForm"  >
                <el-form-item label="id" style="display: none">
                    <el-input v-model="viplevelForm.id" ></el-input>
                </el-form-item>
                <el-form-item label="等级名称" prop="name">
                    <el-input v-model="viplevelForm.name" placeholder=""></el-input>
                </el-form-item>
                <el-form-item label="积分增减比">
                        <el-input v-model="viplevelForm.ratio" placeholder="请输入积分增减比"></el-input>
                </el-form-item>
                <el-form-item label="成长值">
                    <el-input v-model="viplevelForm.growthPoint" placeholder="请输入成长值"></el-input>
                </el-form-item>
                <el-form-item label="默认等级">
                    <el-select v-model="viplevelForm.defaultStatus" placeholder="请选择是否为默认等级">
                        <el-option
                                v-for="item in defaultoptions"
                                :key="item.defaultvalue"
                                :label="item.label"
                                :value="item.defaultvalue">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="免运费标准">
                    <el-input v-model="viplevelForm.freeFreightPoint" placeholder="请输入免运费标准"></el-input>
                </el-form-item>
                <el-form-item label="评论获奖励特权">
                    <el-select v-model="viplevelForm.priviledgeComment" placeholder="请选择是否有评论获奖励特权" >
                        <el-option
                                v-for="item in commentoptions"
                                :key="item.commentvalue"
                                :label="item.label"
                                :value="item.commentvalue">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="专享活动特权">
                    <el-select v-model="viplevelForm.priviledgePromotion" placeholder="请选择是否有专享活动特权">
                        <el-option
                                v-for="item in promotionoptions"
                                :key="item.promotionvalue"
                                :label="item.label"
                                :value="item.promotionvalue">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="会员价格特权">
                    <el-select v-model="viplevelForm.priviledgeMemberPrice" placeholder="请选择是否有会员价格特权">
                        <el-option
                                v-for="item in priceoptions"
                                :key="item.pricevalue"
                                :label="item.label"
                                :value="item.pricevalue">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="生日特权">
                    <el-select v-model="viplevelForm.priviledgeBirthday" placeholder="请选择是否有生日特权">
                        <el-option
                                v-for="item in birthdayoptions"
                                :key="item.birthdayvalue"
                                :label="item.label"
                                :value="item.birthdayvalue">
                        </el-option>
                    </el-select>
                </el-form-item>

            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="formSubmit">确 定</el-button>
              </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "VipLevel",
        data(){
            //唯一性校验
            var Id = this.id
            var same = {id: '', same: ''}
            var that = this
            var validatorname = function (rule, value, callback) {
                same.id = Id;
                same.same = value;
                that.$http.post(`/manage/level/uniqueCheck`, same).then(function (response) {
                    if (response.data.result.type) {
                        callback();
                    } else {
                        callback("菜单名重复");
                    }
                });
            }
            return{
                editDialogVisible:false,
                pageSize:5,
                currentPage:1,
                tableData:[],
                viplevelFormData: {},
                total:0,
                id:'',
                viplevelForm:{
                    name:'',
                },
                //name唯一
                rules:{
                    name: [
                        { required: true, message: '请输入等级名称', trigger: 'blur' },
                        { min: 1, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' },
                        {validator: validatorname, trigger: 'blur'}
                    ],
                },

                defaultvalue:'',
                defaultoptions: [{
                    freevalue: 0,
                    label: '0-不是'
                }, {
                    freevalue: 1,
                    label: '1-是'
                },],
                commentvalue:'',
                commentoptions: [{
                    commentvalue: 0,
                    label: '0-没有'
                }, {
                    commentvalue: 1,
                    label: '1-有'
                },],
                promotionvalue:'',
                promotionoptions: [{
                    promotionvalue: 0,
                    label: '0-没有'
                }, {
                    promotionvalue: 1,
                    label: '1-有'
                },],
                pricevalue:'',
                priceoptions: [{
                    pricevalue: 0,
                    label: '0-没有'
                }, {
                    pricevalue: 1,
                    label: '1-有'
                },],
                birthdayvalue:'',
                birthdayoptions: [{
                    birthdayvalue: 0,
                    label: '0-没有'
                }, {
                    birthdayvalue: 1,
                    label: '1-有'
                },],

                ratiovalue:'',
                ratioptions: [{
                    ratiovalue: 1,
                    label: '1-普通'
                }, {
                    ratiovalue: 1.2,
                    label: '1.2-白银'
                },{
                    ratiovalue: 1.5,
                    label: '1.5-黄金'
                },],
            }
        },
        created() {
            this.initLevelTable()
        },
        methods:{
            initLevelTable(){
                var that = this;
                this.$http.post(`/manage/level/initVipLevel/${this.currentPage}/${this.pageSize}`,this.viplevelFormData).then(function (resp) {
                    that.tableData=resp.data.result.records;
                    that.total = resp.data.result.total;
                })
            },
            statusFormat: function( row, column) {
                return row.defaultStatus == '1' ? "是" : row.defaultStatus == '0' ? "是" : "否";
            },
            commentFormat: function( row, column) {
                return row.priviledgeComment == '1' ? "是" : row.priviledgeComment == '0' ? "是" : "否";
            },
            promotionFormat: function( row, column) {
                return row.priviledgePromotion == '1' ? "是" : row.priviledgePromotion == '0' ? "是" : "否";
            },
            memberPriceFormat: function( row, column) {
                return row.priviledgeMemberPrice == '1' ? "是" : row.priviledgeMemberPrice == '0' ? "是" : "否";
            },
            birthdayFormat: function( row, column) {
                return row.priviledgeBirthday == '1' ? "是" : row.priviledgeBirthday == '0' ? "是" : "否";
            },

            formSubmit(){
                var that=this;
                if(this.viplevelForm.id!=null){
                    this.$http.put("/manage/level/updateLevel",this.viplevelForm).then(function (returnResult) {
                        that.editDialogVisible=false;
                        that.viplevelForm={};
                        that.initLevelTable();
                    })
                }else {
                    this.$http.post("/manage/level/insertLevel", this.viplevelForm).then(function (returnResult) {
                        that.editDialogVisible = false;
                        that.initLevelTable();
                    })
                }
            },

            selectByVipId(id){
                var that=this;
                this.$http.post(`/manage/level/selectByVipId/${id}`).then(function (returnResult) {
                    that.editDialogVisible=true;
                    that.viplevelForm=returnResult.data.result.VipInfo;
                })
            },

            handleSizeChange(v){
                this.pageSize = v;
                this.currentPage();
            },
            //切换页码时触发该方法
            handleCurrentChange(v){
                this.currentPage = v;
                this.initLevelTable();
            },
            quere() {
                this.initLevelTable()
            },

            insertClient(){
                this.editDialogVisible=true;
                this.viplevelForm={};
            },
            updateViplevel(){
                this.editDialogVisible=true;
                this.viplevelForm={};
                that.carTypeForm=returnResult.data.result.InfoVip;
                console.log(returnResult)
            },
            deleteViplevel(id){
                var that=this;
                this.$http.delete(`/manage/level/deleteVipLevel/${id}`, this.viplevelForm).then(function (resp) {
                    that.initLevelTable();
                })
            },

        }

    }
</script>

<style scoped>

</style>